using System.Collections.Generic;
using System.Linq;
using Provausio.Common.Objects;
using TrainingManagement.api.Models.BusinessLogic.SkillSet;
using TrainingManagement.api.Models.BusinessObjects;
using TrainingManagement.api.Models.Data.CRUD;
using TrainingManagement.api.Models.Data.CRUD.Position;

namespace TrainingManagement.api.Models.BusinessLogic.Position
{
    public class PostionSkillSetSelectOperation : BusinessOperation<int, List<BusinessObjects.SkillSet>>
    {
        readonly PositionSkillSetSelectDataSource _skillSetSelect = new PositionSkillSetSelectDataSource();
        readonly SkillSetSelectOperation _skillSetSelectOperation = new SkillSetSelectOperation();

        public override List<BusinessObjects.SkillSet> Run(int input)
        {
            return _skillSetSelect.Execute(input);
        }

        public List<BusinessObjects.SkillSet> Run(int input, bool loadSkills)
        {
            var results = _skillSetSelect.Execute(input);
            if (!loadSkills) return results;
            foreach (var skillSet in results)
            {
                var firstOrDefault = _skillSetSelectOperation.Run(skillSet.SkillSetId, true).FirstOrDefault();
                if (firstOrDefault != null)
                    skillSet.SkillSetItems = firstOrDefault.SkillSetItems;
            }
            return results;
        } 
    }
}